package com.au.bescene.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;

import com.au.bescene.R;
import com.au.bescene.adapter.PhotoGalleryAdapter;

/**
 * Represents Photo Gallery.
 * @author Khaled
 *
 */
public class PhotoGallery extends Activity {
		
	private ArrayList<Bitmap> _pictures = new ArrayList<Bitmap>();
	private PhotoGalleryAdapter _photoGalleryAdapter;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photogallery);
		
		createGallery();
	}

	/**
	 * Initializes and creates the Photo Gallery.
	 */
	private void createGallery() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		Bundle extras = getIntent().getExtras();
		String folderName = extras.getString("foldername");

		Gallery gallery = (Gallery) findViewById(R.id.photogallery_gallery);
		_photoGalleryAdapter = new PhotoGalleryAdapter(this, _pictures, folderName);
		gallery.setAdapter(_photoGalleryAdapter);

		MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
		mlp.setMargins(-(metrics.widthPixels/2), 
		               mlp.topMargin, 
		               mlp.rightMargin, 
		               mlp.bottomMargin
		);
		
		ImageView imageView = (ImageView) findViewById(R.id.photogallery_imageView);
		imageView.setImageBitmap(_photoGalleryAdapter.getImageRessource(0));

		gallery.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				ImageView imageView = (ImageView) findViewById(R.id.photogallery_imageView);
				
				imageView.setImageBitmap(_photoGalleryAdapter.getImageRessource(position));

			}
		});
	}
}